<template>
    <el-dialog :visible.sync="voiceVisible" title="设备开机自动运行配置" width="80%" center @close="voiceVisible = false">
        <div style="margin: 0px auto; text-align: left;width: 80%;">
            <br />
            <el-card class="box-card1" style="padding-left: 5px;margin-bottom: 5px;">
                <el-switch v-model="value1" active-text="开机自启动" inactive-text="关闭开机启动">
                </el-switch>
            </el-card>
        </div>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="voiceVisible = false">取消</el-button>
                <el-button type="primary" @click="submit">保存</el-button>
            </span>
        </template>
    </el-dialog>
</template>
<script lang='ts'>
import { IDevice } from '@/api/types';
import Vue from 'vue'
import { DevicesModule } from '@/store/modules/device'

export default Vue.extend({
    props: [],
    data() {
        return {
            voiceVisible: false,
            value1: true,
            device: { deviceId: "", autoOpen: 1, },
        }
    },
    methods: {
        submit() {
            let v1 = this.value1 ? 1 : 0;
            console.error(v1);
            if (this.device.autoOpen && v1 || !this.device.autoOpen && !v1) {
                this.voiceVisible = false;
                return;
            }
            this.device.autoOpen = v1;
            DevicesModule.setAutoOpen({ id: this.device.deviceId, auto: v1 + "" }).then((v: any) => {
                console.error(v)
                if (v >= 1) {
                    this.voiceVisible = false;
                }
            }).catch(e => {
                console.error(e);
            })
        },
        showDialog(d: IDevice) {
            this.device = d;
            this.value1 = d.autoOpen ? true : false;
            this.voiceVisible = true;
        },
    }
})


</script>

<style>

</style>

